package org.gruposp2p.aularest.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.gruposp2p.aularest.server.LinkConstants;

/**
 *
 * @author jj
 */
@XmlType
@XmlRootElement(name = "itemcalificabletype")
@XmlAccessorType(XmlAccessType.NONE)
@Entity
@Table(name = "ITEMCALIFICABLETYPE", catalog = "database", schema = "PUBLIC")
@NamedQueries({@NamedQuery(name = "Itemcalificabletype.findAll", query = "SELECT i FROM Itemcalificabletype i"), @NamedQuery(name = "Itemcalificabletype.findById", query = "SELECT i FROM Itemcalificabletype i WHERE i.id = :id"), @NamedQuery(name = "Itemcalificabletype.findByName", query = "SELECT i FROM Itemcalificabletype i WHERE i.name = :name"), @NamedQuery(name = "Itemcalificabletype.findByPercent", query = "SELECT i FROM Itemcalificabletype i WHERE i.percent = :percent"), @NamedQuery(name = "Itemcalificabletype.findByDescription", query = "SELECT i FROM Itemcalificabletype i WHERE i.description = :description")})
public class Itemcalificabletype implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "NAME", nullable = false, length = 48)
    private String name;
    @Column(name = "PERCENT", precision = 10, scale = 2)
    @XmlElement(name="percent")
    private BigDecimal percent;
    @Basic(optional = false)
    @Column(name = "DESCRIPTION", nullable = false, length = 511)
    @XmlElement(name="description")
    private String description;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "itemcalificabletypeId")
    private Collection<Itemcalificable> itemcalificableCollection;

    @Transient
    @XmlElement(name="itemcalificabletypeLink")
    private Link link;

    public Link getLink() {
        return link;
    }

    public void setLink(Link link) {
        this.name = link.getName();
        this.link = link;
    }

    public Itemcalificabletype() {
    }

    public Itemcalificabletype(Integer id) {
        this.id = id;
    }

    public Itemcalificabletype(Integer id, String name, String description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPercent() {
        return percent;
    }

    public void setPercent(BigDecimal percent) {
        this.percent = percent;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Collection<Itemcalificable> getItemcalificableCollection() {
        return itemcalificableCollection;
    }

    public void setItemcalificableCollection(Collection<Itemcalificable> itemcalificableCollection) {
        this.itemcalificableCollection = itemcalificableCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Itemcalificabletype)) {
            return false;
        }
        Itemcalificabletype other = (Itemcalificabletype) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.gruposp2p.restletjpa.model.Itemcalificabletype[id=" + id + "]";
    }
    @PostLoad
    public void postProcess() {
        link = new Link();
        link.setHref(LinkConstants.getItemcalificabletypeLink(id.toString()));
        link.setId(id.toString());
        link.setName(name);
    }

    @PrePersist
    public void prePersist () {
        this.name = link.getName();
    }

}
